package code1_100.code11_20;

/**
 * @author 韩迪
 * @date 2022/03/21 21:31
 **/
public class Code19 {


    public class ListNode {
          int val;
          ListNode next;
          ListNode() {}
          ListNode(int val) { this.val = val; }
          ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode h = new ListNode();
            //运用双指针解删除链表中倒数第n个节点
            ListNode pre = new ListNode();
            h = pre;
            ListNode del = head;
            pre.next = del;
            ListNode tail = head;
            for (int i = 0; i < n; i++) {
                tail = tail.next;
            }
            while (tail!=null){
                pre = pre.next;
                tail = tail.next;
                del = del.next;
            }
            //找到了要删除的节点，删除del
            pre.next = del.next;
            return h.next;
        }
    }
}
